package com.lrr.controller;

import com.github.pagehelper.PageInfo;
import com.lrr.entity.QueryVo;
import com.lrr.entity.Water;
import com.lrr.service.WaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class WaterController {

    @Autowired
    WaterService service;

    @RequestMapping("/list")
    public String list(QueryVo vo, @RequestParam(defaultValue = "1") Integer pageNum, Model model){
        PageInfo<Water> info = service.list(vo,pageNum);
        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        return "water_list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Water water,Model model){
        model.addAttribute("water",water);
        return "water_add";
    }

    @ResponseBody
    @RequestMapping("/selectById")
    public Integer selectById(String name){
        return service.selectById(name);
    }

    @RequestMapping("/add")
    public String add(@Validated @ModelAttribute("water") Water water, BindingResult result,Model model){
        if (result.hasErrors()){
            model.addAttribute("water",water);
            return "water_add";
        }
        service.add(water);
        return "redirect:list";
    }
}
